//
//  LHPhotoBrowserCell.swift
//  sinaWeibo
//
//  Created by LiHong on 15/11/8.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit

class LHPhotoBrowserCell: UICollectionViewCell {
    
    //MARK: - 属性
    var url: NSURL? {
        didSet {
            guard let imageUrl = url else {
                print("url为空")
                return
            }
            
            imageView.image = nil
            indicator.startAnimating()
            
            self.imageView.sd_setImageWithURL(imageUrl) { (image, error, _, _) -> Void in
                self.indicator.stopAnimating()
                if error != nil {
                    print("下载图片出错error = \(error)")
                    return
                }
                
                print("下载成功")
                
                self.layoutImageView(image)
            }
            
            
        }
    }
    
    ///根据长短图，重新布局imageView
    private func layoutImageView(image: UIImage) {
        let size = displaySize(image)
        //当长度小于屏幕长度
        if size.height < UIScreen.mainScreen().bounds.height {
            let offsetY = (UIScreen.mainScreen().bounds.height - size.height) * 0.5
            imageView.frame = CGRect(x: 0, y: offsetY, width: size.width, height: size.height)
        } else {
            //长图
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            
            scrollView.contentSize = size
        }
        
        
        
    }
    
    ///重新计算尺寸
    private func displaySize(image: UIImage) -> CGSize {
        let newWidth = UIScreen.mainScreen().bounds.width
        
        let newHeight = newWidth * image.size.height / image.size.width
        
        return CGSize(width: newWidth, height: newHeight)
    }
    
    //MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 准备UI
    func prepareUI() {
        contentView.addSubview(scrollView)
        scrollView.addSubview(imageView)
        contentView.addSubview(indicator)
        scrollView.delegate = self
        
        scrollView.maximumZoomScale = 2
        scrollView.minimumZoomScale = 0.5
        
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        indicator.translatesAutoresizingMaskIntoConstraints = false
        
        let views = ["sv" : scrollView]
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[sv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[sv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views))
        
        //indicator
        contentView.addConstraint(NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: indicator, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
    }
    
    
    
    //MARK: - 懒加载
    ///scrollView
    private var scrollView = UIScrollView()
    
    ///imageView
    private var imageView = UIImageView()
    
    ///下载指示器
    private var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
}

extension LHPhotoBrowserCell: UIScrollViewDelegate {
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
}
